using System;
using System.Configuration;
using System.Data;
using digit.Core;

namespace SMS.ServiceManager
{
    public class PartnerManager
    {
        #region CONSTRUCTORS
        static PartnerManager()
        {
            _data = new PartnerData(Util.GetConnectionString("SMSSERVICE"));
            _error = "";
        }
        #endregion

        #region ATTRIBUTES
        private static PartnerData _data;
        private static string _error;
        #endregion

        #region PROPERTIES
        public static string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        #endregion

        #region METHODS
        public static bool Insert(string partnerName, string partnerCode, string commandCode, string serviceId, bool status)
        {
            _error = "";
            try
            {
                object[] values = new object[5];
                values[0] = Util.ReplaceNullTo(partnerName, DBNull.Value);
                values[1] = Util.ReplaceNullTo(partnerCode, DBNull.Value);
                values[2] = Util.ReplaceNullTo(commandCode, DBNull.Value);
                values[3] = Util.ReplaceNullTo(serviceId, DBNull.Value);
                values[4] = status;

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Insert(PartnerInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[5];
                values[0] = Util.ReplaceNullTo(info.PartnerName, DBNull.Value);
                values[1] = Util.ReplaceNullTo(info.PartnerCode, DBNull.Value);
                values[2] = Util.ReplaceNullTo(info.CommandCode, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.ServiceID, DBNull.Value);
                values[4] = info.Status;

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(int partnerId, string partnerName, string partnerCode, string commandCode, string serviceId, bool status)
        {
            _error = "";
            try
            {
                object[] values = new object[6];
                values[0] = partnerId;
                values[1] = Util.ReplaceNullTo(partnerName, DBNull.Value);
                values[2] = Util.ReplaceNullTo(partnerCode, DBNull.Value);
                values[3] = Util.ReplaceNullTo(commandCode, DBNull.Value);
                values[4] = Util.ReplaceNullTo(serviceId, DBNull.Value);
                values[5] = status;

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(PartnerInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[6];
                values[0] = info.PartnerID;
                values[1] = Util.ReplaceNullTo(info.PartnerName, DBNull.Value);
                values[2] = Util.ReplaceNullTo(info.PartnerCode, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.CommandCode, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info.ServiceID, DBNull.Value);
                values[5] = info.Status;

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(int PartnerID)
        {
            _error = "";
            try
            {
                object[] values = { PartnerID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(PartnerInfo info)
        {
            _error = "";
            try
            {
                object[] values = { info.PartnerID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static PartnerInfo Select(int PartnerID)
        {
            _error = "";
            try
            {
                object[] values = { PartnerID };
                DataTable dTable = _data.Select(values);
                if (dTable.Rows.Count < 1) return null;


                PartnerInfo info = new PartnerInfo(dTable.Rows[0]);
                return info;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAll()
        {
            _error = "";
            try
            {
                return _data.SelectAll();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAllByCode(string partnerCode)
        {
            _error = "";
            try
            {
                return _data.SelectAllByCode(partnerCode);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        #endregion

    }
}